package com.tiance.demo.ui.property;

import com.tiance.demo.observer.ObserverData;
import com.tiance.demo.observer.ObserverEvent;
import com.tiance.demo.observer.ObserverSystem;
import com.tiance.demo.observer.Publisher;
import com.tiance.demo.service.mysql.bo.FileHeader;
import com.tiance.demo.service.mysql.bo.Page;
import com.tiance.demo.ui.vo.ByteItem;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.util.Observable;
import java.util.Observer;

public class FileHeaderPropertyTable extends TableView<ByteItem> implements Observer {

    public FileHeaderPropertyTable() {
        setPrefHeight(305);
        setEditable(false);
        getSelectionModel().setSelectionMode(SelectionMode.SINGLE); //多选模式
        setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);//列居左

        TableColumn propKey = new TableColumn("Property");
        TableColumn propValue = new TableColumn("Value");
        TableColumn propSize = new TableColumn("Size");
        TableColumn propOffset = new TableColumn("Offset");
        getColumns().addAll(propKey, propValue, propSize, propOffset);

        propKey.setPrefWidth(120);
        propKey.setCellValueFactory(new PropertyValueFactory<ByteItem, String>("key"));
        propKey.setSortable(false);

        propValue.setPrefWidth(200);
        propValue.setCellValueFactory(new PropertyValueFactory<ByteItem, String>("value"));
        propValue.setSortable(false);

        propSize.setPrefWidth(50);
        propSize.setCellValueFactory(new PropertyValueFactory<ByteItem, Integer>("size"));
        propSize.setSortable(false);

        propOffset.setPrefWidth(70);
        propOffset.setCellValueFactory(new PropertyValueFactory<ByteItem, Integer>("offset"));
        propOffset.setSortable(false);

        ObserverSystem.registerObserver(ObserverEvent.PAGE_SWITCH, this);
    }

    @Override
    public void update(Observable o, Object arg) {
        Publisher publisher = (Publisher) o;
        ObserverData transfer = ObserverData.transfer(arg);

        if (publisher.getObserverEvent() == ObserverEvent.PAGE_SWITCH) {
            Page page = (Page) transfer.getData();
            FileHeader fileHeader = page.getFileHeader();

            ObservableList<ByteItem> list = FXCollections.observableArrayList();

            int offset = 0;
            list.add(new ByteItem("Check Sum", fileHeader.getSpaceOrChksum(), 4, offset));
            list.add(new ByteItem("Page No", String.valueOf(fileHeader.getOffset()), 4, offset += 4));
            list.add(new ByteItem("Prev Page No", String.valueOf(fileHeader.getPrev()), 4, offset += 4));
            list.add(new ByteItem("Next Page No", String.valueOf(fileHeader.getNext()), 4, offset += 4));
            list.add(new ByteItem("LSN", fileHeader.getLsn(), 8, offset += 4));
            list.add(new ByteItem("Page Type", fileHeader.getType(), 2, offset += 8));
            list.add(new ByteItem("File Flush LSN", fileHeader.getFileFlushLsn(), 8, offset += 2));
            list.add(new ByteItem("Table Space", String.valueOf(fileHeader.getArchLogNoOrSpaceId()), 4, offset += 8));

            setItems(list);
        }
    }
}
